package handler

import (
	"book-shelf-mgr/cache"
	"book-shelf-mgr/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func DeliveryOrderListDetail(c *gin.Context) {
	id := c.Query("id")
	var list model.DeliveryOrderList
	redis_key := "delivery_order_list_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("delivery_orders").
			Select(
				"delivery_orders.id OrderId",
				"deliveries.delivery_no DeliveryNo",
				"deliveries.delivery_name DeliveryName",
				"deliveries.delivery_tel DeliveryTel",
				"delivery_orders.order_no OrderNo",
				"delivery_orders.user_name UserName",
				"delivery_orders.order_address OrderAddress",
			).
			Joins("LEFT JOIN deliveries ON delivery_orders.did = deliveries.id").
			Where("delivery_orders.id=?", id).Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": list,
	})
	return
}

/*
SELECT

	delivery_orders.id OrderId,
	deliveries.delivery_no DeliveryNo,
	deliveries.delivery_name DeliveryName,
	deliveries.delivery_tel DeliveryTel,
	delivery_orders.order_no OrderNo,
	delivery_orders.user_name UserName,
	delivery_orders.order_address OrderAddress

FROM

	delivery_orders
	LEFT JOIN deliveries ON delivery_orders.did = deliveries.id
*/
func DeliveryOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var list []model.DeliveryOrderList
	redis_key := "delivery_order_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("delivery_orders").
			Select(
				"delivery_orders.id OrderId",
				"deliveries.delivery_no DeliveryNo",
				"deliveries.delivery_name DeliveryName",
				"deliveries.delivery_tel DeliveryTel",
				"delivery_orders.order_no OrderNo",
				"delivery_orders.user_name UserName",
				"delivery_orders.order_address OrderAddress",
			).
			Joins("LEFT JOIN deliveries ON delivery_orders.did = deliveries.id").
			Offset(offset).Limit(size).Debug().Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var total int64
	model.DB.Model(&model.DeliveryOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  list,
		"total": total,
	})
	return
}

func DeliveryOrderAdd(c *gin.Context) {
	did, _ := strconv.Atoi(c.PostForm("did"))
	order_no := c.PostForm("order_no")
	user_name := c.PostForm("user_name")
	order_address := c.PostForm("order_address")
	data := model.DeliveryOrder{
		Did:          did,
		OrderNo:      order_no,
		UserName:     user_name,
		OrderAddress: order_address,
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	var deilveryOrder model.DeliveryOrder
	redis_key := "delivery_order_" + strconv.Itoa(did)
	model.DB.Where("id=?", data.ID).Find(&deilveryOrder)
	jsonData, _ := json.Marshal(deilveryOrder)
	cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})
	return
}

func DeliveryAdd(c *gin.Context) {
	delivery_no := c.PostForm("delivery_no")
	delivery_name := c.PostForm("delivery_name")
	delivery_tel := c.PostForm("delivery_tel")
	data := model.Delivery{
		DeliveryNo:   delivery_no,
		DeliveryName: delivery_name,
		DeliveryTel:  delivery_tel,
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})
	return
}

func RodeNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.Query("rid"))
	var nodes model.Node
	/*
		SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON role_nodes.nid = nodes.id
		WHERE
		  role_nodes.rid = 1
	*/
	model.DB.Table("nodes").
		Joins(" LEFT JOIN role_nodes ON role_nodes.nid = nodes.id").
		Where("role_nodes.rid = ?", rid).Debug().
		Find(&nodes)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": nodes,
	})
	return
}

func UserRode(c *gin.Context) {
	uid, _ := strconv.Atoi(c.Query("uid"))
	var role model.Role
	/*
		SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON user_roles.rid = roles.id
		WHERE
		  user_roles.uid = 1
	*/
	model.DB.Table("roles").
		Joins(" LEFT JOIN user_roles ON user_roles.rid = roles.id").
		Where("user_roles.uid = ?", uid).Debug().
		Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": role,
	})
	return
}

func RodeNole(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	data := model.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "角色与权限关联失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "角色与权限关联成功",
	})
	return
}
